<template>
  <div class="attention">
    <div class="lizi0">
      <!-- 这里使用v-for遍历 -->
      <div class="yonghu" v-for="(item, index) in attlistdata" :key="index">
        <div class="yonghu_top">
          <div class="touxiang">
            <img class="auto-img" :src="item.u.p" alt="" />
          </div>
          <div class="yhm_right">
            <span>{{ item.u.n }}</span> <i>Lv.{{ item.u.lvl }}</i>
          </div>
          <div @click="goattentionlist(item.u.id)">
            <div ref="yes" class="btn" @click="getyes(item.u.id)">关注</div>
          </div>
        </div>
        <div @click="gobtn(item.item_id)" class="yonghu_center">
          <img class="auto-img" :src="item.img" alt="" />
        </div>
        <div class="yonghu_bottom">
          <div class="bottom">
            <div class="bottom-sc">
              <span
                >{{ item.collect_count_text }} {{ item.views_count_text }}</span
              >
            </div>
          </div>

          <div class="title">{{ item.trim_title }}</div>
        </div>
      </div>
      <div class="btn" @click="btnadd">点击加载更多</div>
      <!-- ============== -->
    </div>
  </div>
</template>

<script>
export default {
  name: "Attention",
  data() {
    return {
      attlistdata: [],
      index: 1,
      arr: [],
      cloll: "",
      isguan: false,
    };
  },

  created() {
    this.getProductType();
  },
  methods: {
    // 点击加关注
    goattentionlist(val) {
      let getid = JSON.parse(localStorage.getItem("attentionlist"));
      let arr=[]
      if (getid != undefined) {
        getid.forEach((ele) => {
          arr.push(ele);
        });
        if (arr.indexOf(val) == -1) {
          arr.push(val);
        }
      } else {
        arr.push(val);
      }
      localStorage.setItem("attentionlist", JSON.stringify(arr));
    },
    getyes(id) {
      
      let texts = document.querySelectorAll(".btn");
      if (this.isguan == false) {
        this.attlistdata.forEach((ele, index) => {
          if (ele.u.id == id) {
            this.$nextTick(() => {
              this.$refs.yes[index].style.backgroundColor = "#ccc";
              texts[index].innerHTML = "已关注";
              this.isguan = true;
            });
          }
        });
      } else {
        this.attlistdata.forEach((ele, index) => {
          if (ele.u.id == id) {
            this.$nextTick(() => {
              this.$refs.yes[index].style.backgroundColor = "#feca2d";
              texts[index].innerHTML = "关注";
              this.isguan = false;
            });
          }
        });
      }
    },
    getProductType() {
      this.axios({
        method: "get",
        url: "/home/ffeeds/0/10",
      })
        .then((result) => {
          // console.log("Attention", result.data.result.rfs);
          this.attlistdata = result.data.result.rfs;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 加载更多

    btnadd() {
      this.index++;
      let num = this.index * 10;
      //   console.log(num);
      this.axios({
        method: "get",
        url: `/home/ffeeds/${num}/10`,
      })
        .then((result) => {
          let res = result.data.result.rfs;
          //   console.log(res);
          res.forEach((ele, index) => {
            if (ele.type === 1) {
              this.attlistdata.push(ele);
            }
          });
          //   console.log(this.tuijian);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    gobtn(id) {
      // console.log(id);
      this.$router.push({ name: "menu", params: { id } });
    },
  },
};
</script>

<style lang="less" scoped>
.attention {
  width: 100%;
  height: 519px;
  overflow: auto;
}
.attention::-webkit-scrollbar {
  width: 0 !important;
}
.lunbo {
  width: 100%;
  height: 100px;
  background-color: aqua;
}

.lizi0 {
  width: 100%;
  height: 100%;
  .yonghu {
    width: 100%;
    min-height: 330px;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    margin-bottom: 10px;
    // background-color: antiquewhite;

    .yonghu_top {
      width: 100%;
      height: 50px;
      display: flex;
      justify-content: space-evenly;
      //   background-color: #ed39de;
      border-top: 1px solid transparent;
      .touxiang {
        float: left;
        margin-top: 5px;
        margin-left: 10px;
        width: 35px;
        height: 35px;
        // background-color: antiquewhite;
        overflow: hidden;
        border-radius: 50%;
        border: 1px solid #ccc;
      }
      .yhm_right {
        float: left;
        margin-left: 15px;
        margin-top: 18px;
        span {
          font-size: 0.4rem;
          font-weight: bold;
        }
        i {
          color: rgb(214, 153, 19);
          font-size: 0.11rem;
        }
      }
      .btn {
        margin-top: 7px;
        width: 65px;
        height: 35px;
        line-height: 35px;
        margin-left: 100px;
        border-radius: 10px;
        background-color: #feca2d;
      }
    }
    .yonghu_center {
      width: 100%;
      height: 70%;
      //   background-color: #39edd5;
    }
    .yonghu_bottom {
      margin-top: 15px;
      width: 100%;
      height: 15%;
      .bottom {
        width: 90%;
        height: 20px;
        // background-color: blue;
        display: flex;
        justify-content: space-between;
        padding: 0 5%;
      }
      .title {
        margin-top: 8px;
        margin-left: 15px;
        width: 90%;
        padding: 0 5%;
        height: 30px;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
  }
}
.btn {
  width: 100%;
  text-align: center;
}
.auto-img {
  width: 100%;
  display: block;
}
</style>